package com.isae.chattingapp;

import java.util.ArrayList;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ChattingActivity extends Activity {
	
	String name;
	String username;
	String CurrentName;
	EditText MessageText;
	private Handler mHandler = new Handler();
	private ArrayList<String> messages = new ArrayList<String>();
	private XMPPConnection ChattingXmppConnection;
	private ListView MessagesView;
	private String domain = "@ofdomain";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Hiding the activity title
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_chatting);

		this.ChattingXmppConnection = XMPPLogic.getInstance().getConnection();
		
		Intent intent = getIntent();
        name     = intent.getStringExtra(ListRosters.NAME_EXTRA_INFO);
        username = intent.getStringExtra(ListRosters.USERNAME_EXTRA_INFO) + domain;        
        CurrentName = XMPPLogic.getInstance().getLoggedInUsername().toUpperCase();
        
        MessageText = ((EditText) findViewById(R.id.Message));
        
        TextView tv = (TextView) findViewById(R.id.ChattingWith);
        tv.setText( getString(R.string.SpeakingWith) + " " + name);
        
        MessagesView = (ListView) findViewById(R.id.MessagesList);
        
        Prepare();
	}
	
	private void Prepare(){   
	    setListAdapter();
//	    Set a listener to send a chat text message
	    Button send = (Button) findViewById(R.id.sendBtn);
	    send.setOnClickListener(new View.OnClickListener() {
	      public void onClick(View view) {
	    	  String text = MessageText.getText().toString();
	    	  if (text.length() != 0) {
		    	  String to = username;
		    	  Message msg = new Message(to, Message.Type.chat);  
		    	  msg.setBody(text);
		    	  ChattingXmppConnection.sendPacket(msg);
		    	  MessageText.setText("");
		          messages.add(CurrentName+ ": " +text);
		          setListAdapter();
	    	  }   
	      }
	      });
	    addMessagesListener();
	}

  private void setListAdapter() {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.uniquemessage, messages);
    MessagesView.setAdapter(adapter);
  }
	
  public void addMessagesListener(){
	// Add a packet listener to get messages sent to us
	  PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
	  ChattingXmppConnection.addPacketListener(new PacketListener() {
        @Override
        public void processPacket(Packet packet) {
          Message message = (Message) packet;
          if (message.getBody() != null) {
            String fromName = StringUtils.parseBareAddress(message.getFrom());
            String ChattingWithUsername = username;
            if (fromName.equalsIgnoreCase(ChattingWithUsername)){
	            messages.add(name + ": "+message.getBody());
	            // Add the incoming message to the list view
	            mHandler.post(new Runnable() {
	              public void run() {
	                setListAdapter();
	              }
	            });
            }    
          }
        }
      }, filter);
  }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chatting, menu);
		return true;
	}

}
